package blogTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author starsea
 * @date 2024-08-13 10:46
 */

public class BlogTest extends InitAndEnd {
    //注册测试用例
    @ParameterizedTest
   @CsvFileSource(resources = "register.csv")
    public void register(String name,String word,String url) {
        //跳转到注册页面
        webDriver.get("http://127.0.0.1:9999/register.html");
        //获取用户名输入框
        WebElement username=webDriver.findElement(By.cssSelector("#username"));
        //注册框中输入用户名
       username.sendKeys(name);
        //获取密码输入框
        WebElement password=webDriver.findElement(By.cssSelector("#password"));
        //输入注册的密码
        password.sendKeys(word);
        //获取GitHub输入框
        WebElement github=webDriver.findElement(By.cssSelector("#Github"));
        //输入注册的github
        github.sendKeys(url);
        //获取注册按钮
        WebElement register=webDriver.findElement(By.cssSelector("#submit"));
        //点击注册按钮
       register.click();
       //等待页面跳转
       webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //校验是不是跳转到登陆页面
       Assertions.assertEquals("http://127.0.0.1:9999/login.html",webDriver.getCurrentUrl(),"注册成功");
    }
    //登录测试用例
    @ParameterizedTest
    @CsvFileSource(resources = "login.csv")
    public void login(String name,String word)
    {
        //获取用户名输入框
        WebElement username=webDriver.findElement(By.cssSelector("#username"));
        //注册框中输入用户名
        username.sendKeys(name);
        //获取密码输入框
        WebElement password=webDriver.findElement(By.cssSelector("#password"));
        //输入注册的密码
        password.sendKeys(word);
        //输入万能验证码
        WebElement ma=webDriver.findElement(By.cssSelector("#identify-input"));
        ma.sendKeys("123456");
        //获取登录按钮
        WebElement login=webDriver.findElement(By.cssSelector("#submit"));
        //点击登录按钮
        login.click();
        //等待页面跳转
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //校验是不是跳转到登陆页面
        Assertions.assertEquals("http://127.0.0.1:9999/blog_list.html",webDriver.getCurrentUrl(),"注册成功");
    }
    //编写博客测试用例
    @ParameterizedTest
    @CsvFileSource(resources = "write.csv")
    public void write(String name,String word,String title,String content) {
        //deng
        //点击编写博客按钮
        WebElement write = webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
        //等待跳转
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //找到标题按钮并填写标题document.querySelector("title").value="test"
        webDriver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
        //找到编辑框并输入内容
        //webDriver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-gutters > div.CodeMirror-gutter.CodeMirror-foldgutter")).sendKeys(content);


        //找到发布按钮并点击
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //校验页面上是不是存在该文章
        List<WebElement> elements = webDriver.findElements(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));
        for (WebElement element : elements) {
            Assertions.assertEquals(element.getText(), title);
        }

    }

}
